package com.jiguang.property.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiguang.property.annotation.Log;
import com.jiguang.property.dto.UserInfoDTO;
import com.jiguang.property.entity.OperateLog;
import com.jiguang.property.service.OperateLogService;
import com.jiguang.property.utils.IpAddressUtil;
import com.jiguang.property.utils.JwtUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Component
@Aspect //切面类
public class SysAspect {

    @Autowired
    private JwtUtils tokenManager;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private OperateLogService operateLogService;

    //切点表达式
    @Pointcut("@annotation(com.jiguang.property.annotation.Log)")
    private void pointcut(){

    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        OperateLog operateLog = new OperateLog();
        //通过工具类获取Request对象
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        //获取用户名
        //需要通过解析token获取
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            return joinPoint.proceed();
        }
        String userId = tokenManager.getUserFromToken(token);
        //从redis中获取用户信息
        String redisKey = "login:" + userId;
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        String cacheObject = (String) redisTemplate.opsForValue().get(redisKey);
        if (StringUtils.isEmpty(cacheObject)) {
            return joinPoint.proceed();
        }
        UserInfoDTO ucenter = new ObjectMapper().readValue(cacheObject.toString(), UserInfoDTO.class);
        operateLog.setUserId(ucenter.getId());

        //获取被增强类和方法
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        //获取被增强的方法对象
        Method method = methodSignature.getMethod();
        //从方法中解析注解
        if (method != null){
            Log logAnnotation = method.getAnnotation(Log.class);
            operateLog.setName(logAnnotation.name());
        }
        //方法名字
        String methodName = method.getName();

        //访问的url
        String url = request.getRequestURI().toString();
        operateLog.setUrl(url);
        //请求方式
        String requestMethod = request.getMethod();
        operateLog.setRequestWay(requestMethod);

        //登陆IP
        String ipAddr = IpAddressUtil.getIpAddr(request);
        operateLog.setLoginIp(ipAddr);

        //保存到数据库
        operateLogService.save(operateLog);

        return joinPoint.proceed();
    }

}
